package com.yotoodesign.dianba.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.yotoodesign.dianba.R;
import com.yotoodesign.dianba.app.GlobalApplication;
import com.yotoodesign.dianba.bean.Song;
import com.yotoodesign.dianba.ui.MainActivity;

public class MySongAdapter extends BaseAdapter {

	private Context     mContext;
	private ArrayList<Song> songList;
	
	
	public ArrayList<Song> getSongList() {
		return songList;
	}

	public void setSongList(ArrayList<Song> songList) {
		this.songList = songList;
	}

	public MySongAdapter(Context c){
		this.mContext = c;
	}
	
	@Override
	public int getCount() {
		return songList!=null?songList.size():0;
	}

	@Override
	public Object getItem(int position) {
		return songList!=null?songList.get(position):null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = LayoutInflater.from(mContext).inflate(R.layout.my_song_render, null);
		TextView songName = (TextView)view.findViewById(R.id.mySongName);
		TextView singer = (TextView)view.findViewById(R.id.singer);
		
		final Song song = (Song)songList.get(position);
		songName.setText(song.getName());
		singer.setText(song.getSinger().getName());
		
		
		//移除歌曲
		ImageButton btnRemoveSong = (ImageButton)view.findViewById(R.id.btnRemoveSong);
		if(btnRemoveSong!=null){
			view.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					MainActivity activity = (MainActivity)GlobalApplication.activity;
					activity.removeSong(song);
					activity.getCurrentFragment().updateListView();
				}
			});
		}

		return view;
	}
	

}
